{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# coding:utf-8\n",
    "import re\n",
    "import requests\n",
    "\n",
    "# 登陆地址\n",
    "url='https://www.yiichina.com/login'\n",
    "# 签到地址\n",
    "url2='https://www.yiichina.com/registration'\n",
    "\n",
    "# 构造登陆的header头\n",
    "headers = {\n",
    "    'Accept': \"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8\",\n",
    "    'Accept-Encoding': \"gzip, deflate, br\",\n",
    "    'Accept-Language': \"zh-CN,zh;q=0.9,en;q=0.8\",\n",
    "    'Cache-Control': \"no-cache\",\n",
    "    'Connection': \"keep-alive\",\n",
    "    'Content-Length': \"250\",\n",
    "    'Content-Type': \"application/x-www-form-urlencoded\",\n",
    "    'Host': \"www.yiichina.com\",\n",
    "    'Origin': \"https://www.yiichina.com\",\n",
    "    'Pragma': \"no-cache\",\n",
    "    'Referer': \"https://www.yiichina.com/login\",\n",
    "    'Upgrade-Insecure-Requests': \"1\",\n",
    "    'User-Agent': \"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) \",\n",
    "    \"Chrome/67.0.3396.99 Safari/537.36 \"\n",
    "}\n",
    "\n",
    "# 构造签到的header头\n",
    "headers = {\n",
    "    'Accept': \"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8\",\n",
    "    'Accept-Encoding': \"gzip, deflate, br\",\n",
    "    'Accept-Language': \"zh-CN,zh;q=0.9,en;q=0.8\",\n",
    "    'Cache-Control': \"no-cache\",\n",
    "    'Connection': \"keep-alive\",\n",
    "    'Content-Length': \"250\",\n",
    "    'Content-Type': \"application/x-www-form-urlencoded\",\n",
    "    'Host': \"www.yiichina.com\",\n",
    "    'Origin': \"https://www.yiichina.com\",\n",
    "    'Pragma': \"no-cache\",\n",
    "    'Referer': \"https://www.yiichina.com/\",\n",
    "    'User-Agent': \"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) \",\n",
    "    \"Chrome/67.0.3396.99 Safari/537.36\",\n",
    "    'X-CSRF-Token': \"jiZGgvngdp0HiiclI4SheJlJr5-lVyymIFgbfE1ZUw33FWv2mI5DzyrJFUFHt8tO_h3az84GH9xEHEtFCh8cfg==\",\n",
    "    'X-Requested-With': \"XMLHttpRequest\"\n",
    "}\n",
    "\n",
    "# 登陆方法和签到\n",
    "def login_sign():\n",
    "    r_session = requests.Session()\n",
    "    page = r_session.get(url)\n",
    "    reg = r'<meta name=\"csrf_token\" content=\"(.+)\">'\n",
    "    csrf = re.findall(reg, page.content)[0]\n",
    "    data = {\n",
    "        \"_csrf\":csrf,\n",
    "        \"LoginForm[username]\": \"52o@qq52o.cn\",\n",
    "        \"LoginForm[password]\": \"shenyan5487.\",\n",
    "        'LoginForm[rememberMe]': \"0\",\n",
    "        \"LoginForm[rememberMe]\": \"1\",\n",
    "        \"login - button\": \"\"\n",
    "    }\n",
    "    \n",
    "    # 完成用户登录\n",
    "    response1 = r_session.post(url, data=data, headers=headers)\n",
    "    #print(response1.content)  #去掉注释 这里就可以看到是否模拟登陆成功\n",
    "    \n",
    "    page2 = r_session.get(url2)\n",
    "    reg = r'<meta name=\"csrf-token\" content=\"(.+)\">'\n",
    "    csrf = re.findall(reg, page2.content)[0]\n",
    "    data2 = {\n",
    "        \"_csrf\": csrf\n",
    "    }\n",
    "    # 模拟签到请求\n",
    "    response2 = r_session.post(url2, data=data2, headers=headers2)\n",
    "    print(response2.content)\n",
    "    \n",
    "if __name__ = \"__main__\":\n",
    "    login_sign()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "在服务器执行 crontab -e"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
